package com.sphd.education.services;

import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.sphd.education.info.HistoryInfo;
import com.sphd.education.info.LessonInfo;
import com.sphd.education.info.SchoolInfo;

import org.litepal.crud.ClusterQuery;
import org.litepal.crud.DataSupport;

import java.util.Date;
import java.util.List;

public class LessonSaveService extends Service {
    public LessonSaveService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("service","onBind==========================");
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("service","onCreate==========================");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("service", "onDestroy=======================");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("service","onUnbind=======================");
        return super.onUnbind(intent);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle bundle = intent.getBundleExtra("lesson");
        if (bundle.getInt("falg")==1){
            LessonInfo lessonInfo = (LessonInfo) bundle.getSerializable("lessoninfo");
           List<LessonInfo> lessonInfos =DataSupport.where("lesson_id = ?",lessonInfo.getLesson_id()+"").find(LessonInfo.class);
            if (lessonInfos.size()==0){
                HistoryInfo historyInfo = new HistoryInfo();
                historyInfo.setLessonInfo(lessonInfo);
                historyInfo.setType(1);//1是课程2是学校
                historyInfo.setCollect_history(1);//1是历史记录2是收藏
                historyInfo.save();
                lessonInfo.setLessonDate(new Date(System.currentTimeMillis()));
                lessonInfo.save();
            }else {
                DataSupport.deleteAll(LessonInfo.class,"lesson_id = ?",lessonInfo.getLesson_id()+"");
                HistoryInfo historyInfo = new HistoryInfo();
                historyInfo.setLessonInfo(lessonInfo);
                historyInfo.setType(1);//1是课程2是学校
                historyInfo.setCollect_history(1);//1是历史记录2是收藏
                historyInfo.save();
                lessonInfo.setLessonDate(new Date(System.currentTimeMillis()));
                lessonInfo.save();
            }
        }else if (bundle.getInt("falg")==2){
            SchoolInfo schoolInfo = (SchoolInfo) bundle.getSerializable("schoolInfo");
            List<SchoolInfo> schoolInfos =DataSupport.where("school_id=?",schoolInfo.getSchool_id()+"").find(SchoolInfo.class);
            if (schoolInfos.size()==0){
                HistoryInfo historyInfo = new HistoryInfo();
                historyInfo.setSchoolInfo(schoolInfo);
                historyInfo.setType(2);//1是课程2是学校
                historyInfo.setCollect_history(1);//1是历史记录2是收藏
                historyInfo.save();
                schoolInfo.setSeeDate(new Date(System.currentTimeMillis()));
                schoolInfo.save();
            }else {
                DataSupport.deleteAll(SchoolInfo.class,"school_id = ?", schoolInfo.getSchool_id() + "");
                HistoryInfo historyInfo = new HistoryInfo();
                historyInfo.setSchoolInfo(schoolInfo);
                historyInfo.setType(2);//1是课程2是学校
                historyInfo.setCollect_history(1);//1是历史记录2是收藏
                historyInfo.save();
                schoolInfo.setSeeDate(new Date(System.currentTimeMillis()));
                schoolInfo.save();
            }
        }
        Log.e("service", "onStartCommand=======================");
        return Service.START_STICKY;
    }
}
